using System;
using System.IO;
using System.Reflection;
using System.Xml;
using System.Xml.Serialization;
using SharePoint.ImportExport.Core;
using ExchangePropertyTags;
namespace Exchange.Export.DataHandler
{
	/// <summary>
	/// Summary description for SiteTemplate.
	/// </summary>
	public class ListTemplates
	{
		private string filename;
		private ExchangePropertiesList tags;
		private ExchangeList generalList=null;
		public ListTemplates(string path)
		{
			filename=path+"\\listtemplates.xml";
		}
		//public bool CreateTemplate(Params p)
		public bool CreateTemplates(string xmlpath, multiValueCollection mvc)
		{
			tags=new ExchangePropertyTags.ExchangePropertiesList();
			FileStream fs =new FileStream(xmlpath,FileMode.Open);
			XmlTextReader xr=new XmlTextReader(fs);
			XmlSerializer xmls=new XmlSerializer(typeof(ExchangePropertiesList));
			tags=(ExchangePropertiesList) xmls.Deserialize(xr);
			xr.Close();
			fs.Close();

			if(tags.Items.Count==0)
				return false;

			generalList=tags.Items[0];

            ListOfContentTypes listTemplates = new ListOfContentTypes();

			bool bRetValue=false;
			ContentType listTemplate=new ContentType();
			

			if(createAnnouncements(listTemplate))
				listTemplates.ContentTypesCollection.Add(listTemplate);

			listTemplate=new ContentType();
			if(createEvents(listTemplate))
                listTemplates.ContentTypesCollection.Add(listTemplate);
			
			listTemplate=new ContentType();
			if(createContacts(listTemplate))
                listTemplates.ContentTypesCollection.Add(listTemplate);

			listTemplate=new ContentType();
			if(createDiscussions(listTemplate))
                listTemplates.ContentTypesCollection.Add(listTemplate);

			listTemplate=new ContentType();
			if(createDocumentLibrary(listTemplate))
                listTemplates.ContentTypesCollection.Add(listTemplate);

			listTemplate=new ContentType();
			if(createTask(listTemplate))
                listTemplates.ContentTypesCollection.Add(listTemplate);
			
			listTemplate=new ContentType();
			if(createAdministration(listTemplate))
                listTemplates.ContentTypesCollection.Add(listTemplate);

			addMultiValues(mvc, listTemplates);
			listTemplates.Serialize(filename);

			bRetValue=true;
			return bRetValue;
		}
		private bool createDocumentLibrary(ContentType listTemplate)
		{
			listTemplate.Name="Exchange documents";
			listTemplate.Title="Exchange documents";
			listTemplate.BaseType="DocumentLibrary";
			listTemplate.Description="This is the list containing the documents";
			listTemplate.EnableAttachments=false;
			listTemplate.EnableVersioning=false;
			foreach(ExchangeProperty prop in generalList.Props)
			{
				if(bool.Parse(prop.SPSBuiltIn))
					continue;
				Field field= new Field();
				field.Name=prop.SPSFieldName;
				field.DisplayName=prop.SPSFieldName;
				field.ExtendedType=(FieldType)Enum.Parse(typeof(FieldType),prop.SPSFieldType,false);
				field.Required=false;
				field.AddToDefaultView=true;
				field.Richtext=false;
				field.GroupBy=false;
				listTemplate.Fields.Add(field);
			}
			Field fieldAuthor= new Field();
			fieldAuthor.Name="Original Author";
			fieldAuthor.DisplayName="Originator";
			fieldAuthor.ExtendedType=FieldType.Text;
			fieldAuthor.Required=false;
			fieldAuthor.AddToDefaultView=true;
			fieldAuthor.Richtext=false;
			fieldAuthor.GroupBy=false;
			listTemplate.Fields.Add(fieldAuthor);
			foreach(ExchangeList list in tags.Items)
			{
				if(list.Name=="Exchange documents")
				{
					foreach(ExchangeProperty prop in list.Props)
					{
						if(bool.Parse(prop.SPSBuiltIn))
							continue;
						Field field= new Field();
						field.Name=prop.SPSFieldName;
						field.DisplayName=prop.SPSFieldName;
						field.ExtendedType=(FieldType)Enum.Parse(typeof(FieldType),prop.SPSFieldType,false);
						field.Required=false;
						field.AddToDefaultView=true;
						field.Richtext=false;
						field.GroupBy=false;
						listTemplate.Fields.Add(field);
					}
				}
			}

			return(true);
		}
		private bool createDiscussions(ContentType listTemplate)
		{
			listTemplate.Name="Exchange Discussions";
            listTemplate.Title = "Exchange Discussions";
			listTemplate.BaseType="DiscussionBoard";
			listTemplate.Description="This is the list containing the threaded discussions";
			listTemplate.EnableAttachments=true;
			listTemplate.EnableVersioning=false;

			Field field= new Field();
			field.Name="Original Author";
			field.DisplayName="Originator";
			field.ExtendedType=FieldType.Text;
			field.Required=false;
			field.AddToDefaultView=true;
			field.Richtext=false;
			field.GroupBy=false;
			listTemplate.Fields.Add(field);
			return(true);
		}

		private bool createEvents(ContentType listTemplate)
		{
			listTemplate.Name="Exchange Events";
			listTemplate.Title="Exchange Events";
			listTemplate.BaseType="Events";
			listTemplate.Description="This is the list containing events";
			listTemplate.EnableAttachments=true;
			listTemplate.EnableVersioning=false;
			foreach(ExchangeProperty prop in generalList.Props)
			{
				
				if(bool.Parse(prop.SPSBuiltIn))
					continue;
				Field field= new Field();
				field.Name=prop.SPSFieldName;
				field.DisplayName=prop.SPSFieldName;
				field.ExtendedType=(FieldType)Enum.Parse(typeof(FieldType),prop.SPSFieldType,false);
				field.Required=false;
				field.AddToDefaultView=true;
				field.Richtext=false;
				field.GroupBy=false;
				listTemplate.Fields.Add(field);
			}
			Field fieldAuthor= new Field();
			fieldAuthor.Name="Original Author";
			fieldAuthor.DisplayName="Originator";
			fieldAuthor.ExtendedType=FieldType.Text;
			fieldAuthor.Required=false;
			fieldAuthor.AddToDefaultView=true;
			fieldAuthor.Richtext=false;
			fieldAuthor.GroupBy=false;
			listTemplate.Fields.Add(fieldAuthor);
			foreach(ExchangeList list in tags.Items)
			{
				if(list.Name=="Exchange Events")
				{
					foreach(ExchangeProperty prop in list.Props)
					{
						if(bool.Parse(prop.SPSBuiltIn))
							continue;
						Field field= new Field();
						field.Name=prop.SPSFieldName;
						field.DisplayName=prop.SPSFieldName;
						field.ExtendedType=(FieldType)Enum.Parse(typeof(FieldType),prop.SPSFieldType,false);
						field.Required=false;
						field.AddToDefaultView=true;
						field.Richtext=false;
						field.GroupBy=false;
						listTemplate.Fields.Add(field);
					}
				}
			}			

			return(true);
		}
		private bool createTask(ContentType listTemplate )
		{
			listTemplate.Name="Exchange Tasks";
			listTemplate.Title="Exchange Tasks";
			listTemplate.BaseType="Tasks";
			listTemplate.Description="This is the list containing Tasks";
			listTemplate.EnableAttachments=true;
			listTemplate.EnableVersioning=false;
		

			foreach(ExchangeProperty prop in generalList.Props)
			{
				if(bool.Parse(prop.SPSBuiltIn))
					continue;
				Field field= new Field();
				field.Name=prop.SPSFieldName;
				field.DisplayName=prop.SPSFieldName;
				field.ExtendedType=(FieldType)Enum.Parse(typeof(FieldType),prop.SPSFieldType,false);
				field.Required=false;
				field.AddToDefaultView=true;
				field.Richtext=false;
				field.GroupBy=false;
				listTemplate.Fields.Add(field);
			}
			Field fieldAuthor= new Field();
			fieldAuthor.Name="Original Author";
			fieldAuthor.DisplayName="Originator";
			fieldAuthor.ExtendedType=FieldType.Text;
			fieldAuthor.Required=false;
			fieldAuthor.AddToDefaultView=true;
			fieldAuthor.Richtext=false;
			fieldAuthor.GroupBy=false;
			listTemplate.Fields.Add(fieldAuthor);
			foreach(ExchangeList list in tags.Items)
			{
				if(list.Name=="Exchange Tasks")
				{
					foreach(ExchangeProperty prop in list.Props)
					{
						if(bool.Parse(prop.SPSBuiltIn))
							continue;
						Field field= new Field();
						field.Name=prop.SPSFieldName;
						field.DisplayName=prop.SPSFieldName;
						field.ExtendedType=(FieldType)Enum.Parse(typeof(FieldType),prop.SPSFieldType,false);
						field.Required=false;
						field.AddToDefaultView=true;
						field.Richtext=false;
						field.GroupBy=false;
						listTemplate.Fields.Add(field);
					}
				}
			}			
			return(true);
		}
		private bool createAnnouncements(ContentType listTemplate )
		{
			listTemplate.Name="Exchange Announcements";
			listTemplate.Title="Exchange Announcements";
			listTemplate.BaseType="Announcements";
			listTemplate.Description="This is the list containing Announcements";
			listTemplate.EnableAttachments=true;
			listTemplate.EnableVersioning=false;
			Field field= new Field();
			field.Name="Original Author";
			field.DisplayName="Originator";
			field.ExtendedType=FieldType.Text;
			field.Required=false;
			field.AddToDefaultView=true;
			field.Richtext=false;
			field.GroupBy=false;
			listTemplate.Fields.Add(field);
			return true;
		}
		private bool createAdministration(ContentType listTemplate )
		{
			listTemplate.Name="Exchange Administration Team Folders";
			listTemplate.Title="ExAdministration";
			listTemplate.BaseType="Announcements";
			listTemplate.Description="This is the list containing Team Folder Administration posts";
			listTemplate.EnableAttachments=true;
			listTemplate.EnableVersioning=false;
			Field field= new Field();
			field.Name="Original Author";
			field.DisplayName="Originator";
			field.ExtendedType=FieldType.Text;
			field.Required=false;
			field.AddToDefaultView=true;
			field.Richtext=false;
			field.GroupBy=false;
			listTemplate.Fields.Add(field);
			return true;
		}
		public bool addMultiValues (multiValueCollection mvc, ListOfContentTypes ltl)
		{
			PropertyValuesCollection pvc = new PropertyValuesCollection();
			if(mvc.Count==0)
			{
				multiValue mval=new multiValue();
				mval.spFieldName="Exchange Categories";
				mval.Value="(none)";
				mvc.Add(mval);
			}

			foreach (ContentType lt in ltl.ContentTypesCollection)
				
			{
				foreach (Field fi in lt.Fields)
				{
				 foreach (multiValue mv in mvc)
					{
						if (fi.Name == mv.spFieldName)
						{
							//pvc = new PropertyValuesCollection();
							fi.PropertyValues.Add(mv.Value);
							
						}
					}
				}
			}
			return(true);//fi.
			
		}

		private bool createContacts(ContentType listTemplate )
		{
			listTemplate.Name="Exchange Contacts";
			listTemplate.Title="Exchange Contacts";
			listTemplate.BaseType="Contacts";
			listTemplate.Description="This is the list containing Contacts";
			listTemplate.EnableAttachments=true;
			listTemplate.EnableVersioning=false;
			foreach(ExchangeProperty prop in generalList.Props)
			{
				if(bool.Parse(prop.SPSBuiltIn))
					continue;
				Field field= new Field();
				field.Name=prop.SPSFieldName;
				field.DisplayName=prop.SPSFieldName;
				field.ExtendedType=(FieldType)Enum.Parse(typeof(FieldType),prop.SPSFieldType,false);
				field.Required=false;
				field.AddToDefaultView=true;
				field.Richtext=false;
				field.GroupBy=false;
				listTemplate.Fields.Add(field);
			}
			Field fieldAuthor= new Field();
			fieldAuthor.Name="Original Author";
			fieldAuthor.DisplayName="Originator";
			fieldAuthor.ExtendedType=FieldType.Text;
			fieldAuthor.Required=false;
			fieldAuthor.AddToDefaultView=true;
			fieldAuthor.Richtext=false;
			fieldAuthor.GroupBy=false;
			listTemplate.Fields.Add(fieldAuthor);
			foreach(ExchangeList list in tags.Items)
			{
				if(list.Name=="Exchange Contacts")
				{
					foreach(ExchangeProperty prop in list.Props)
					{
						
						if(bool.Parse(prop.SPSBuiltIn) && prop.SPSFieldName!="Notes")
							continue;
						Field field= new Field();
						if(prop.SPSFieldName=="Notes")
							field.Name="Comments";
						else
							field.Name=prop.SPSFieldName;
						field.DisplayName=prop.SPSFieldName;
						field.ExtendedType=(FieldType)Enum.Parse(typeof(FieldType),prop.SPSFieldType,false);
						field.Required=false;
						field.AddToDefaultView=(prop.SPSFieldName!="Notes");
						field.Richtext=(prop.SPSFieldName=="Notes");
						field.GroupBy=false;
						listTemplate.Fields.Add(field);
					}
				}
			}			return true;
		}

	}
}
